package com.igeekhome.controller.api;

import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.ReceiveAndDestDto;
import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.DSendingMaintenanceList;
import com.igeekhome.pojo.DSpotDestination;
import com.igeekhome.pojo.SysStaff;
import com.igeekhome.service.IDestService;
import com.igeekhome.service.INetworkService;
import com.igeekhome.service.ISendingMainListService;
import com.igeekhome.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 立以宁
 */
@RestController
@RequestMapping("/api/section")
public class SendingSectionControllerApi {

    @Autowired
    private ISendingMainListService sendingMainLIstService;

    @Autowired
    private INetworkService networkService;

    @Autowired
    private IDestService destService;

    @RequestMapping("/list")
    public TableResultBean list(@RequestParam(defaultValue = "10") Integer limit, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "") String sectionName) {
        PageInfo<ReceiveAndDestDto> pageInfo = sendingMainLIstService.selectByAll(limit, page, sectionName);
        return new TableResultBean(0, "成功", pageInfo.getTotal(), pageInfo.getList());

    }

    @RequestMapping("/edit")
    public ResultBean edit(HttpServletRequest request, HttpSession session) {
        SysStaff staff = (SysStaff)session.getAttribute("staff");
        String rangeName = request.getParameter("rangeName");
        String sectionType = request.getParameter("sectionType");
        String strId = request.getParameter("strId");
        String remark = request.getParameter("remark");
        String id = request.getParameter("id");

        System.out.println("sectionType: "+sectionType);
        DSendingMaintenanceList dSendingMaintenanceList = new DSendingMaintenanceList().setRangeName(rangeName).setRemark(remark).setServicingManId(staff.getStaffId())
                .setServicingNetworkId(staff.getAffiliatedNetworkId()).setStatus(1).setSectionType(Integer.valueOf(sectionType)).setId(Integer.valueOf(id));
        ReceiveAndDestDto receiveAndDestDto = new ReceiveAndDestDto().setDSendingMaintenanceList(dSendingMaintenanceList).setStrId(strId);
        sendingMainLIstService.update(receiveAndDestDto);
        return ResultBean.ok();
    }

    /**
     * 操作人和操作网点从session中取
     *
     * 如果没有修改那么首处不会有逗号
     *
     * 主键为自增，mybatis返回主键值
     */
    @RequestMapping("/add")
    public ResultBean add(HttpServletRequest request, HttpSession session) {
        SysStaff staff = (SysStaff)session.getAttribute("staff");
        String rangeName = request.getParameter("rangeName");
        String sectionType = request.getParameter("sectionType");
        String remark = request.getParameter("remark");
        String strId = request.getParameter("strId");

        DSendingMaintenanceList dSendingMaintenanceList = new DSendingMaintenanceList().setRangeName(rangeName).setRemark(remark).setServicingManId(staff.getStaffId())
                .setServicingNetworkId(staff.getAffiliatedNetworkId()).setStatus(1).setSectionType(Integer.valueOf(sectionType));
        ReceiveAndDestDto receiveAndDestDto = new ReceiveAndDestDto().setDSendingMaintenanceList(dSendingMaintenanceList).setStrId(strId);
        sendingMainLIstService.add(receiveAndDestDto);
        return ResultBean.ok();
    }



    @RequestMapping("/selectUpdate")
    public ResultBean selectUpdate(HttpServletRequest request) {
        List<DtreeNode> dtreeNodes = new ArrayList<>();
        String address = request.getParameter("address");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        DNetwork dNetwork = new DNetwork().setAddressId(address).setNetworkName(name).setNetworkId(Integer.valueOf(id));
        List<DNetwork> dNetworks = networkService.selectByCondition(dNetwork);
        for (DNetwork item : dNetworks
        ) {
            DtreeNode dtreeNode = new DtreeNode();
            Integer networkId = item.getNetworkId();
            String networkName = item.getNetworkName();
            dtreeNode.setTitle(networkName);
            dtreeNode.setId(String.valueOf(networkId));
            dtreeNodes.add(dtreeNode);

        }
        dtreeNodes.add(treeInit());
        return ResultBean.ok(dtreeNodes);
    }


    @RequestMapping("/selectAdd")
    public ResultBean selectAdd() {
        return ResultBean.ok();
    }


    @RequestMapping("/delete")
    public ResultBean delete(Integer id, Integer sectionType) {
        sendingMainLIstService.delete(id, sectionType);
        return ResultBean.ok();
    }


    /**
     * 初始化根节点
     *
     * @return
     */
    public DtreeNode treeInit() {
        DtreeNode dtreeNode = new DtreeNode();
        dtreeNode.setId("001");
        dtreeNode.setTitle("全部");
        dtreeNode.setParentId("0");
        return dtreeNode;
    }

    /**
     * 树结构的复选框进行回显
     * @param ids
     * @param id
     * @return 为true代表选中，遍历没有返回false
     */
    public boolean isChecked(String[] ids, String id) {
        for (String item : ids) {
            if (item.equals(id)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 根据下拉框的不同，返回不同的区间数据
     * sectionType 为0表示寄件区间 为1表示目的区间
     */

    @RequestMapping("/sectionData/{id}")
    public newResultBean sectionData(@PathVariable Integer id, String strId) {
        String[] eId = null;
        if (strId != null) {
            eId = strId.split(",");
        }

        List<DtreeNode> dtreeNodes = new ArrayList<>();

        if (id == 0) {
            List<DNetwork> dNetworks = networkService.selectAll();
            for (DNetwork dNetwork : dNetworks
            ) {
                DtreeNode dtreeNode = new DtreeNode();
                Integer idd = dNetwork.getNetworkId();
                String networkName = dNetwork.getNetworkName();
                String s = String.valueOf(idd);
                if (isChecked(eId, s) && strId != null) {
                    dtreeNode.setCheckArr("1");
                }
                dtreeNode.setId(s);
                dtreeNode.setTitle(networkName);

                dtreeNodes.add(dtreeNode);
            }

            dtreeNodes.add(treeInit());

            return newResultBean.newOk(new Status(200, "成功"), dtreeNodes);

        } else if (id == 1) {

            List<DSpotDestination> getlist = destService.getlist();
            for (DSpotDestination dSpotDestination : getlist
            ) {
                DtreeNode dtreeNode = new DtreeNode();
                Integer id1 = dSpotDestination.getId();
                String name = dSpotDestination.getName();
                String s = String.valueOf(id1);
                if (isChecked(eId, s) && strId != null) {
                    dtreeNode.setCheckArr("1");
                }
                dtreeNode.setTitle(name);
                dtreeNode.setId(s);
                dtreeNodes.add(dtreeNode);
            }
            dtreeNodes.add(treeInit());
            return newResultBean.newOk(new Status(200, "成功"), dtreeNodes);

        }

        return null;
    }


}
